import axios from "axios";
import router from "@/router";
import { ElMessage } from "element-plus";
import { getToken } from "@/utils/auth";
import { loginStore } from "@/store/index";
export function request(config) {
  // 创建axios的实例
  const instance = axios.create({
    baseURL: "http://rap2api.taobao.org/app/mock/255027",
    timeout: 10000,
  });
  // 请求拦截器配置
  instance.interceptors.request.use(
    (config) => {
      const store = loginStore();
      if (store.token) {
        config.headers["X-Token"] = getToken();
      }
      return config;
    },
    (error) => {
      console.log(error);
      return Promise.error(error);
    }
  );
  // 响应拦截器配置
  instance.interceptors.response.use(
    (res) => {
      // const res = response.data;
      // if the custom code is not 20000, it is judged as an error.
      if (res.code == 500) {
        return Promise.reject(new Error(res.message || "Error"));
      } else {
        return res;
      }
    },
    (error) => {
      /* switch (error.response.status) {
      case 401:
        console.log("无权访问")
        router.push({path: '/login'})
        break
      case 403:
        console.log("token过期啦")
        router.push({path: '/login'})
        break
      case 404:
        console.log("404啦")
        router.push({path: '/404'})
        break
      default:
        return Promise.reject(error)
    } */
      return Promise.reject(error);
    }
  );
  // 发送真正的网络请求
  return instance(config);
}
export default request;
